from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from apps.users.models import User

class Comment(models.Model):
        content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) # 与django contenttype表建立外键
        object_id = models.PositiveIntegerField()  # 对应表中的对象id
        content_object = GenericForeignKey('content_type','object_id') # 不会生成列
        text=models.TextField()
        comment_time=models.DateTimeField(auto_now_add=True)
        user=models.ForeignKey(User,on_delete=models.DO_NOTHING)

        root =models.ForeignKey('self',related_name='root_comment',null=True,on_delete=models.DO_NOTHING)

        parent=models.ForeignKey('self',null=True,on_delete=models.DO_NOTHING)
        reply_tp=models.ForeignKey(User,related_name='replies',null=True,on_delete=models.DO_NOTHING)
        def __str__(self):
                return self.text
        class Meta:
           ordering = ['comment_time']